import EventEmitter from 'eventemitter3'

export default promise => {
  const ee = new EventEmitter()
  let ready = true
  return (...args) => new Promise(async (resolve, reject) => {
    ee.once('resolve', resolve)
    ee.once('reject', reject)
    if (ready) {
      ready = false
      try {
        const res = await promise(...args)
        ee.emit('resolve', res)
      } catch (e) {
        ee.emit('reject', e)
      }
      ready = true
    }
  })
}
